// pages/article/article.js
const app = getApp()
const config = require('../../config')
const util = require('../../utils/util')
const restService = require('../../services/restService')

Page({

    /**
     * 页面的初始数据
     */
    data: {
      editorCtx: {},
      moduleList: [],
      moduleCode: "",
      moduleName: "",
      questionTitle: "",
      questionContext: "",
      questionSubTitle: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      this.getModuleList()
    },
    getModuleList: function () {
      restService.commonRequest({
        url: config.service.getStudyModuleList,
        data: {
        },
        method: 'POST',
        success: res => {
            this.setData({
              moduleList: res.data
            });
        },
        fail: err => {
          console.log(err, 'error')
        }
      })
    },
    bindPickerChange: function(e) {
      console.log('picker发送选择改变，携带值为', this.data.moduleList[e.detail.value])
      this.setData({
        moduleCode: this.data.moduleList[e.detail.value].moduleCode,
        moduleName: this.data.moduleList[e.detail.value].moduleName
      })
    },

    onEditorReady() {
      const that = this
      wx.createSelectorQuery().select('#editor').context(function (res) {
          that.editorCtx = res.context
      }).exec()
    },
    onStatusChange(e) {
      const formats = e.detail
      this.setData({
          formats
      })
    },
    undo() {
      this.editorCtx.undo()
    },
    redo() {
      this.editorCtx.redo()
    },
    format(e) {
      let {
          name,
          value
      } = e.target.dataset
      if (!name) return
      this.editorCtx.format(name, value)
    },
    clear() {
      this.editorCtx.clear({
          success: function (res) {
              console.log("clear success")
          }
      })
    },
    onBlur(e){
      console.log(e)
      this.oneEditor.blur()
    },
    insertImage() {
      util.showTip("暂不支持，缺个图片服务器！！")
    },
    inputChange(params) {
      this.setData({
        questionTitle: params.detail.value
      })
    },
    editorInputChange(e){
      console.log(e.detail)
      this.setData({
        questionContext: e.detail.html,
        questionSubTitle: e.detail.text
      })
    },
    formSubmit: function(e) {
    
      restService.commonRequest({
        url: config.service.createQuestion,
        data: {
          moduleCode: this.data.moduleCode,
          title: this.data.questionTitle,
          answer: this.data.questionContext,
          subTitle: this.data.questionSubTitle
        },
        method: 'POST',
        success: res => {
          let detailUrl = config.pages.javaQuestionList + '?moduleCode='+this.data.moduleCode+'&title='+this.data.moduleName
            util.showSuccessTo("success", detailUrl)
        },
        fail: err => {
          console.log(err, 'error')
        }
      })
    }
})